<%@page language="java" contentType="text/html; charset=UTF-8" isELIgnored="false" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="html" tagdir="/WEB-INF/tags" %>
<!DOCTYPE html>
<html>
<html:header title="快捷改价">

    <link rel="stylesheet" href="${baseUrl}css/public.css" media="all">

    <style type="text/css">

    </style>

    <script>
        jQuery(function () {
            ajaxSelect("${baseUrl}goods/type/getAll.action","typeId","商品分类");
        });

        layui.use(['form', 'table'], function () {
            var $ = layui.jquery,
                form = layui.form,
                table = layui.table,
                util = layui.util;
            renderTable();

            table.on('edit(table)', function(obj){
                console.log(obj.value); //得到修改后的值
                console.log(obj.field); //当前编辑的字段名
                console.log(obj.data); //所在行的所有相关数据
                var data = obj.data;
                var field = obj.field;
                if (obj.value == '') {
                    tip("价格不能为空");
                    data[field] = data.goodsUnitVoList[0][field];
                    obj.update(data);
                    return;
                }

                if (!isNumber(obj.value)) {
                    tip("价格必须为数字");
                    data[field] = data.goodsUnitVoList[0][field];
                    obj.update(data);
                    return;
                }

                let price = getPrice(obj.value);
                for (let i=0;i<data.goodsUnitVoList.length; i++){
                    let temp = price * data.goodsUnitVoList[i].times;
                    temp = getPrice(temp);
                    data.goodsUnitVoList[i][field] = temp;

                    delete data.goodsUnitVoList[i].unit;
                }
                console.log(JSON.stringify(data.goodsUnitVoList)); //所在行的所有相关数据

                tipLoad("更新中......");
                post("${baseUrl}goods/goods/updatePrice.action",data.goodsUnitVoList, function (resp) {
                    closeLoading();
                    if (resp.code != 1){
                        tip(resp.msg);
                        // obj.update(resp.data);

                        obj.update(data);
                    } else {
                        tip("更新成功");
                    }
                });
            });

            form.on('submit(search)', function (data) {
                var json = data.field;
                table.reload('table',{
                    where: json
                    ,page: {
                        curr: 1 //重新从第 1 页开始
                    }
                });
                return false;
            });

        });

        function renderTable() {
            layui.table.render({
                id:'table',
                elem: '#table',
                url: "${baseUrl}goods/goods/getList.action",
                where : {
                    status :1,
                    time:(new Date()).valueOf()
                },
                cols: [[
                    {type:'numbers'},
                    {field: 'no', width: 120, title: '编号',style:"font-size:12px;"},
                    {field: 'name', minWidth: 120, title: '名称',style:"font-size:12px;"},
                    {width: 120, title: '单位',style:"font-size:12px;",templet: function(d){
                        return d.goodsUnitVoList[0].unit.name;
                    }},
                    {width: 120, title: '零售价', field:'retailPrice',style:"font-size:12px;", edit:'text',templet: function(d){
                            return getPrice(d.goodsUnitVoList[0].retailPrice);
                    }},
                    {width: 120, title: '批发价', field:'wholesalePrice',style:"font-size:12px;", edit:'text',templet: function(d){
                            return getPrice(d.goodsUnitVoList[0].wholesalePrice);
                    }},
                    {width: 120, title: '最低售价', field:'minPrice',style:"font-size:12px;", edit:'text',templet: function(d){
                            return getPrice(d.goodsUnitVoList[0].minPrice);
                    }},
                    {width: 160, title: '参考进货价', field:'referBuyPrice',style:"font-size:12px;", edit:'text',templet: function(d){
                            return getPrice(d.goodsUnitVoList[0].referBuyPrice);
                    }},
                ]],
                limit: 10,
                page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                    layout: ['limit', 'count', 'prev', 'page', 'next', 'skip'] //自定义分页布局
                    //,curr: 5 //设定初始在第 5 页
                    ,groups: 5 //只显示 5 个连续页码
                    ,first: false //不显示首页
                    ,last: false //不显示尾页
                },
                done: function(res, curr, count){
                    //console.log(JSON.stringify(res));
                    //渲染完成，初始化操作框提示
                    initOperator();

                }
            });
        }
    </script>

</html:header>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <blockquote class="layui-elem-quote layui-text">
            快捷改价功能，修改基本单位的价格后，副单位价格会同时发生改变
        </blockquote>
        <div class="table-tool-div">
            <form class="layui-form layui-form-pane" action="" id="searchForm">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">其他信息</label>
                        <div class="layui-input-inline" style="width: 200px;">
                            <input type="text" id="search" name="search" style="font-size: 12px;" placeholder="编号、名称、规格、属性、条形码" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-inline">
                        <a class="layui-btn " lay-submit="" lay-filter="search" id="btn-search"><i class="layui-icon layui-icon-search"></i></a>
                        <a class="layui-btn " id="selectSearch">筛选</a>
                    </div>

                    <div class="table-tool-div-screen" status="close" style="display: none">
                        <div class="layui-inline">
                            <label class="label">商品分类</label>
                            <div class="layui-input-inline">
                                <select name="typeId">
                                </select>
                            </div>
                        </div>

                        <div class="layui-inline">
                            <label class="label">商品编号</label>
                            <div class="layui-input-inline">
                                <input type="text" name="no" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-inline">
                            <label class="label">商品名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="name" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-inline">
                            <label class="label">商品属性</label>
                            <div class="layui-input-inline">
                                <input type="text" name="attr" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-inline">
                            <label class="label">商品规格</label>
                            <div class="layui-input-inline">
                                <input type="text" name="norms" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-inline">
                            <label class="label">备注</label>
                            <div class="layui-input-inline">
                                <input type="text" name="remark" class="layui-input">
                            </div>
                        </div>

                        <c:if test="${industryType.type eq 2}">
                            <div class="layui-inline">
                                <label class="label">序列号状态</label>
                                <div class="layui-input-inline" style="width: 90px;">
                                    <select name="sn">
                                        <option value="">不限</option>
                                        <option value="true">启用</option>
                                        <option value="false">停用</option>
                                    </select>
                                </div>
                            </div>
                        </c:if>

                        <div class="layui-inline">
                            <a class="layui-btn" lay-submit="" lay-filter="search">搜索</a>
                            <a class="layui-btn layui-btn-primary" id="clearSearch">清空所有条件</a>
                        </div>

                        <script>
                            $("#selectSearch").click(function () {
                                if ($(".table-tool-div-screen").attr("status")=="close") {
                                    $(".table-tool-div-screen").css("display","block");
                                    $(".table-tool-div-screen").attr("status","open");
                                    $("#search").parent().parent().css("display","none");
                                    $("#search").val("");
                                    $("#btn-search").css("display","none");
                                    $(this).html("收起");
                                } else {
                                    $(".table-tool-div-screen").css("display","none");
                                    $(".table-tool-div-screen").attr("status","close");
                                    $("#search").parent().parent().css("display","inline-block");
                                    $("#btn-search").css("display","inline-block");
                                    $(this).html("筛选");
                                }
                            });

                            $("#clearSearch").click(function () {
                                $("#searchForm")[0].reset();
                            });
                        </script>
                    </div>
                </div>
            </form>
        </div>

        <div class="horizon-line"></div>

        <table class="layui-hide" id="table" lay-filter="table" lay-size="sm"></table>
    </div>
</div>

</body>
</html>
<html:loading/>